package com.git.concurrent;

import java.util.concurrent.CountDownLatch;

import sun.applet.Main;

/**
 * 目标:
 * 	1.主线程开启5个worker线程，
 *  2.worker线程等待主线程发出命令，并发执行
	3.主线程发出命令就阻塞自己
 *  2.5个worker线程都执行完，主线程自己唤醒
 * @author wangquan
 *
 */
public class CountDownLatchDemo {
	public static CountDownLatch startSignal = new CountDownLatch(1);
	public static CountDownLatch doneSignal = new CountDownLatch(5);
	
	public static void main(String[] args) throws InterruptedException {
		//启动5个worker线程
		for (int i = 0; i < 5; i++) {
			new Worker().start();
		}
		
		startSignal.countDown();
		//暂停自己线程
		doneSignal.await();
		System.out.println(Thread.currentThread().getName());
		
	}
}
class Worker extends Thread{
	@Override
	public void run() {
		try {
			//都等待命令
			CountDownLatchDemo.startSignal.await();
			System.out.println(Thread.currentThread().getName());
			//workCD--1，减为0时，主线程自动唤醒
			CountDownLatchDemo.doneSignal.countDown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
